package com.bwie.bwedu.presenter.impl;

import android.text.TextUtils;

import com.bwie.bwedu.bean.Student;
import com.bwie.bwedu.bean.response.BaseResponse;
import com.bwie.bwedu.bean.response.MainInfoResponse;
import com.bwie.bwedu.fragment.LocalSaveFragment;
import com.bwie.bwedu.model.AddStuListen;
import com.bwie.bwedu.model.LocalSaveModel;
import com.bwie.bwedu.model.impl.LocalSaveModelImpl;
import com.bwie.bwedu.presenter.LocalSavePresenter;
import com.bwie.bwedu.utils.LogUtils;
import com.bwie.bwedu.utils.PhoneUtil;

/**
 * Created by zhang on 2016/7/24.
 */
public class LocalSavePresenterImpl implements LocalSavePresenter,LocalSaveModel.InvalidListener,LocalSaveModel.Listener,LocalSaveModel.DeleteListerer,AddStuListen {
    private LocalSaveModelImpl model;
    private LocalSaveFragment fragment;

    public LocalSavePresenterImpl(LocalSaveFragment fragment) {
        model = new LocalSaveModelImpl();
        this.fragment = fragment;
    }
    @Override
    public void presenter(String name, String pwd) {
        //调应请求数据方法
        LogUtils.d("----------------presenter---------------------");
        model.http(name,pwd,this);
    }

    @Override
    public void presenter(String pwd) {
        model.Invalidhttp(pwd, this);
    }

    @Override
    public void deletePresenter(String id) {
        model.deleteHttp(id,this);
    }

    @Override
    public void upLoad(Student student) {
        String per=student.gxxm;
        String ptel=student.gxdh;
        String adress=student.jg;
        String id=student.xb;
        String procurator=student.dlrid;
        String stu=student.xm;
        String stel=student.grsj;
        if (TextUtils.isEmpty(student.xm)){
            if (TextUtils.isEmpty(student.gxxm)){
                fragment.onUpLoadFail("学生及联系人为空");
                return;
            }else if (TextUtils.isEmpty(student.gxdh)){
                fragment.onUpLoadFail("请输入联系人电话");
                return;
            }
            else if (!PhoneUtil.isMobileNO(student.gxdh)){
                fragment.onUpLoadFail("请输入合法联系人电话");
                return;
            }
        }else if (TextUtils.isEmpty(student.grsj)){
            if (TextUtils.isEmpty(student.gxxm)||TextUtils.isEmpty(student.gxdh)){
                fragment.onUpLoadFail("请输入学生电话");
                return;
            }
        }
        if (!TextUtils.isEmpty(student.grsj)&&!student.grsj.equals("null")){
            LogUtils.e("=====sss=========="+student.grsj);
            if (!PhoneUtil.isMobileNO(student.grsj)){
                fragment.onUpLoadFail("请输入合法学生电话");
                return;
            }
        }
//        if (!TextUtils.isEmpty(student.grsj)&&student.grsj!="null"&&!PhoneUtil.isMobileNO(student.grsj)){
//            fragment.onUpLoadFail("请输入合法学生电话");
//            return;
//        }

        LogUtils.e("===========pre2============");
        model.Comit(student,this);
    }

    @Override
    public void success(MainInfoResponse userResponse) {
        fragment.onSuccess(userResponse);
        LogUtils.e("sssssssssssssfragment.onSuccess(userResponse);sssssssssssss");
    }

    @Override
    public void error(String info) {
        fragment.onFail(info);
    }

    //确认无效请求成功的方法
    @Override
    public void invalidsuccess(MainInfoResponse userResponse) {
        fragment.InvalidSuccess(userResponse);
    }
    @Override
    public void invaliderror(String info) {
        fragment.InvalidError(info);
    }

    @Override
    public void deleteSuccess(BaseResponse baseResponse) {
        fragment.deleteSuccess(baseResponse);
    }
    @Override
    public void deleteError(String info) {
       fragment.deleteError(info);
    }


    @Override
    public void onSuccess(String id) {
        fragment.onUpLoadSuccess();
    }

    @Override
    public void onFail(String info) {
        fragment.onUpLoadFail("信息上传失败，请检查网络！");
    }
}
